package com.cqwshzj.framework.job;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/7 18:52
 */

import com.cqwshzj.aware.AppContextHolder;
import com.cqwshzj.framework.dao.api.JobErrRptDao;
import com.cqwshzj.framework.dao.api.JobInfoDao;
import com.cqwshzj.framework.dao.api.JobStrategyDao;
import com.cqwshzj.framework.exception.unchecked.JobFrameworkException;
import com.cqwshzj.framework.service.JobErrRptService;
import com.cqwshzj.framework.service.JobInfoService;
import com.cqwshzj.framework.service.JobStrategyService;
import com.dangdang.ddframe.job.api.ShardingContext;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/7
 * @since 1.0.0
 */
public abstract class AbstractSimpleJob implements JobBase{


	private static final Logger log = LoggerFactory.getLogger( AbstractSimpleJob.class );

	//---------------------- other properties ---------
	/** 是否运行 */
	protected volatile  boolean run = true;

	//------------------------ service properties ----
	protected JdbcTemplate jdbcTemplate;
	protected TransactionTemplate transactionTemplate;

	protected JobInfoService jobInfoService;
	protected JobInfoDao jobInfoDao;
	protected JobErrRptDao jobErrRptDao;
	protected JobErrRptService jobErrRptService;
	protected JobStrategyDao jobStrategyDao;
	protected JobStrategyService jobStrategyService;

	//---------------------    ------------

	public void executeJob(ShardingContext shardingContext) {
		initInstanceProperties(shardingContext);
		beforeExecuteJob(shardingContext);
		try {
			if (run) {
				doExecute( shardingContext );
			}
		}catch ( Throwable t){
			Exception exception = null;
			if ( t instanceof  Exception ){
				exception = (Exception) t;
			}else{
				exception = new Exception(t.getMessage(), t);
			}
			handleException( exception );
		}
		afterExecuteJob(shardingContext);
	}

	//---------------------    ----------------------

/*  @Override
	public void doExecute(ShardingContext shardingContext) {

	} */

	//--------------

	@Override
	public void beforeExecuteJob(ShardingContext shardingContext) {
	}


	@Override
	public void afterExecuteJob(ShardingContext shardingContext) {
	}


	@Override
	public void initInstanceProperties(ShardingContext shardingContext) {
		doInitInstanceProperties( shardingContext );
	}

	protected  void doInitInstanceProperties(ShardingContext shardingContext){
		jdbcTemplate = AppContextHolder.getBean( JdbcTemplate.class );
		transactionTemplate = AppContextHolder.getBean( TransactionTemplate.class );

		jobInfoDao = AppContextHolder.getBean( JobInfoDao.class );
		jobInfoService = AppContextHolder.getBean( JobInfoService.class );
		jobErrRptDao = AppContextHolder.getBean( JobErrRptDao.class );
		jobErrRptService = AppContextHolder.getBean( JobErrRptService.class );
		jobStrategyDao = AppContextHolder.getBean( JobStrategyDao.class );
		jobStrategyService = AppContextHolder.getBean( JobStrategyService.class );
	};

	@Override
	public void handleException(Exception e) {
		doHandleException(e);
	}

	protected void doHandleException(Exception e){
		log.error("there is an error happened while executing SimpleJob instance . exception message : "+e.getMessage() ,e);
		if ( e instanceof JobFrameworkException ){
			JobFrameworkException ex = (JobFrameworkException) e;
			String jobId = ex.getJobId();
			if ( StringUtils.isNotEmpty( jobId ) ) {
				addJobErrRptAndSetJobFailed(jobId,ex);
			}
		}
	}

	protected void addJobErrRptAndSetJobFailed(String jobId, JobFrameworkException ex){
		jobInfoService.addJobErrRptAndSetJobFailed(jobId, ex);
	}

}
